from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse, HttpResponseRedirect
import json


# Create your views here.
# ?a=1&b=2&a=3
def weather(request, city, year):
    # print(city)
    # print(year)

    # 查询字符串
    # a = request.GET.get('a')
    # alist = request.GET.getlist('a')
    # b = request.GET.get('b')
    # print(a)
    # print(alist)
    # print(b)

    # 表单类型(只要请求的数控类型是表单类型，无论是哪种请求方式，都可以用request.POST来获取请求体的表单数据)
    # c = request.POST.get("c")
    # clist = request.POST.getlist("c")
    # d = request.POST.get("d")
    # body = request.body
    # print(body)
    # print(c)
    # print(clist)
    # print(d)

    # 非表单类型Json
    json_bytes = request.body
    req_dict = json.loads(json_bytes)
    c = req_dict.get('c')
    d = req_dict.get('d')
    print(c)
    print(d)
    # request.META属性获取请求头Headers中的数据，type:dict
    print(request.META.get("CONTENT_TYPE"))
    print('*' * 10)
    print(request.method)
    print(request.user)
    print(request.path)
    print(request.encoding)
    return HttpResponse("ok")


def demo_view(request):
    response = HttpResponse('ok')
    response.set_cookie('anchnet', 'xian')
    response.set_cookie('anchnet2', 'beijing', max_age=60)
    print(request.COOKIES.get('anchnet'))
    return response


def response_demo(request):
    # ret = {"company": "Anchnet"}
    # response = HttpResponse(ret, content_type='application/json', status=200)
    # response['contet_name'] = 'Jack'
    # return response
    # return HttpResponseNotFound()
    # return JsonResponse(ret)
    # return redirect('http://127.0.0.1:8000/users/index/')
    request.session['user_id'] = 12
    request.session['username'] = 'Jack'
    print(request.session.get('user_id'))
    del request.session['username']
    print(request.session.get('username'))
    # session默认过期时间为两周，set_expiry(value)  valu单位为秒
    request.session.set_expiry(10)
    return HttpResponseRedirect('http://127.0.0.1:8000/users/index/')
